package com.itheima.redbaby.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.HashCodeBuilder;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.BaseActivity.MyHttpTask;
import com.itheima.redbaby.dao.domain.AddressDo;
import com.itheima.redbaby.engine.AddressEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.PromptManager;
import com.lidroid.xutils.view.annotation.ViewInject;

public class AddressUpdateActivity extends BaseActivity implements OnClickListener {
	
	private EditText name;
	private EditText phone;
	private EditText addr;
	private EditText addrdetail;
	private AddressDo address;
	private TextView tv_add_address_back;
	private TextView tv_add_address_save;
	private TextView address_manage_default_btn;
	private TextView address_manage_delete_btn;
	private AddressEngine impl;
	private Map<String,Object> addmap;
	
	private String areaValue;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.address_update_activity);
		
		initView();
	}

	private void initView() {
		impl = DaoAndEngineFactory.getImpl(AddressEngine.class);
		
		
		
		Intent intent=getIntent();
		Bundle extras = intent.getExtras();
		address = (AddressDo) extras.getSerializable("address");
		
		name=(EditText) findViewById(R.id.address_listitem_receiver_text);
		phone=(EditText) findViewById(R.id.address_listitem_phone_text);
		addr=(EditText) findViewById(R.id.address_listitem_ads_text);
		addrdetail=(EditText) findViewById(R.id.address_listitem_adsdetail_text);
		
		tv_add_address_back = (TextView) findViewById(R.id.tv_add_address_back);
		tv_add_address_save = (TextView) findViewById(R.id.tv_add_address_save);
		address_manage_default_btn = (TextView) findViewById(R.id.address_manage_default_btn);
		address_manage_delete_btn = (TextView) findViewById(R.id.address_manage_delete_btn);
		
		tv_add_address_back.setOnClickListener(this);
		tv_add_address_save.setOnClickListener(this);
		address_manage_default_btn.setOnClickListener(this);
		address_manage_delete_btn.setOnClickListener(this);
		
		showDetail();
	}

	private void showDetail() {
		
		new MyHttpTask() {
			
			@Override
			protected Object doInBackground(Void... params) {
				areaValue = impl.getAreaValue(address.getAreaId());
				return null;
			}
			protected void onPostExecute(Object result) {
				name.setText(address.getName());
				phone.setText(address.getPhonenumber());
				addr.setText(areaValue);
				addrdetail.setText(areaValue+address.getAreaDetail());
			};
		}.executeHttp();
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_add_address_back:
			finish();
			break;
		case R.id.tv_add_address_save:
			saveAddress();
			break;
		case R.id.address_manage_default_btn:
			defaultAddress();
			break;
		case R.id.address_manage_delete_btn:
			deleteAddress();
			break;

		default:
			break;
		}
		
	}

	private void defaultAddress() {
		new MyHttpTask() {
			
			@Override
			protected Object doInBackground(Void... params) {
				return impl.setDefultAddress(address.getId());
			}
			protected void onPostExecute(Object result) {
				boolean b=(Boolean) result;
				if(b){
					PromptManager.showToast(AddressUpdateActivity.this, "设置成功");
					finish();
				}else{
					PromptManager.showToast(AddressUpdateActivity.this, "设置失败");
				}
			};
			
		}.executeHttp();
		
	}
	private void deleteAddress() {
		new MyHttpTask() {
			
			@Override
			protected Object doInBackground(Void... params) {
				return impl.deleteAddress(address.getId());
			}
			protected void onPostExecute(Object result) {
				boolean b=(Boolean) result;
				if(b){
					PromptManager.showToast(AddressUpdateActivity.this, "删除成功");
					finish();
				}else{
					PromptManager.showToast(AddressUpdateActivity.this, "删除失败");
				}
			};
			
		}.executeHttp();
		
	}

	private void saveAddress() {
		addmap = new HashMap<String, Object>();
		addmap.put("id", address.getId());
		addmap.put("name", name.getText().toString().trim());
		addmap.put("phonenumber", phone.getText().toString().trim());
		addmap.put("areadetail", addrdetail.getText().toString().trim().substring(areaValue.length()));
		addmap.put("areaid", address.getAreaId());
		addmap.put("fixedtel", address.getFixedtel());
		addmap.put("zipcode", address.getZipCode());
		
		
		new MyHttpTask() {
			
			@Override
			protected Object doInBackground(Void... params) {
				return impl.insertAddress(addmap);
				
			}
			protected void onPostExecute(Object result) {
				List<AddressDo> addressList=(List<AddressDo>) result;
				if(addressList.size()>0){
					PromptManager.showToast(AddressUpdateActivity.this, "修改成功");
					
					setResult(Activity.RESULT_OK);
					finish();
				}else{
					PromptManager.showToast(AddressUpdateActivity.this, "修改失败");
					
				}
			};
		}.executeHttp();
	}

}
